//
//  IndexHeaderController.h
//  yigou
//
//  Created by SunChao on 17/1/16.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "FamilyGroupCell.h"
#import "HeaderContent.h"
#import "contentLikeModel.h"
#import "TopicContentModel.h"
#import "ContentCmtModel.h"
#import "ContentListModel.h"
#define nameFont [UIFont systemFontOfSize:15]
#define tribeFont [UIFont systemFontOfSize:13]
#define textFont [UIFont systemFontOfSize:15]
#define timeFont [UIFont systemFontOfSize:12]
#define replyFont [UIFont systemFontOfSize:13]

@interface FamilyGroupCell()

@end
@implementation FamilyGroupCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"FamilyGroupCell";
    
    //缓存中取
    FamilyGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    //创建
    if (!cell)
    {
        cell = [[FamilyGroupCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //cell.userInteractionEnabled = NO;  //不让用户点击
    return cell;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //让子定义cell和系统cell一样，一创建出来就有一些子控件给我们使用
        //创建头像
        self.selectionStyle = UITableViewCellSelectionStyleNone;   //cell选中时的颜色，无色
        
        UIImageView *iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        //创建昵称
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.font = nameFont;
        nameLabel.textColor = [UIColor colorWithHexString:@"#212121"];
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        //发布于
        //        UILabel *texLabel = [[UILabel alloc]init];
        //        texLabel.font = nameFont;
        //        texLabel.textColor = [UIColor colorWithRed:77/255.0 green:182/255.0 blue:172/255.0 alpha:1];
        //        [self.contentView addSubview:texLabel];
        //        texLabel.text = @"发布于:";
        //        self.texLabel = texLabel;
        //部落来源
        UILabel *tribeLabel = [[UILabel alloc]init];
        tribeLabel.font = tribeFont;
        tribeLabel.textColor = [UIColor colorWithHexString:@"0EA1B6"];
        [self.contentView addSubview:tribeLabel];
        self.tribeLabel = tribeLabel;
        //关注按钮
        UIButton *guanzhuBtn = [UIButton buttonWithType:0];
        [guanzhuBtn setBackgroundImage:[UIImage imageNamed:@"seen"] forState:UIControlStateNormal];
        [self.contentView addSubview:guanzhuBtn];
        self.guanzhuBtn = guanzhuBtn;
        //创建正文
        UILabel *shuoshuotextLabel = [[UILabel alloc]init];
        shuoshuotextLabel.font = textFont;
        shuoshuotextLabel.textColor = [UIColor colorWithHexString:@"#616161"];
        shuoshuotextLabel.numberOfLines = 0;
        [self.contentView addSubview:shuoshuotextLabel];
        self.shuoshuotextLabel = shuoshuotextLabel;
        //大图片
        UIImageView *bigImg = [[UIImageView alloc]init];
        [self.contentView addSubview:bigImg];
        self.bigImg = bigImg;
        self.bigImg.layer.masksToBounds = YES;
        self.bigImg.contentMode = UIViewContentModeScaleAspectFill;
        //创建时间戳
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = timeFont;
        timeLabel.textColor = [UIColor colorWithHexString:@"#9E9E9E"];
        [self.contentView addSubview:timeLabel];
        self.timeLabel = timeLabel;
        //创建分享按钮
        UIButton *shareButton = [UIButton buttonWithType:0];
        [shareButton setImage:[UIImage imageNamed:@"seen"] forState:0];
        [self.contentView addSubview:shareButton];
        self.shareButton = shareButton;
        //创建评论按钮
        UIButton *replyButton = [UIButton buttonWithType:0];
        [replyButton setImage:[UIImage imageNamed:@"reply"] forState:0];
        [self.contentView addSubview:replyButton];
        self.replyButton = replyButton;
        //点赞按钮
        UIButton *dianzanBtn = [UIButton buttonWithType:0];
        [dianzanBtn setImage:[UIImage imageNamed:@"点赞"] forState:0];
        [self.contentView addSubview:dianzanBtn];
        self.dianzanBtn = dianzanBtn;
        //创建评论的背景
        UIImageView *replyBackgroundView = [[UIImageView alloc]init];
        replyBackgroundView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:replyBackgroundView];
        self.replyBackgroundView = replyBackgroundView;
        //点击更多评论
        UIButton *moreButton = [UIButton buttonWithType:0];
        [self.contentView addSubview:moreButton];
        [moreButton setTitleColor:[UIColor colorWithHexString:@"#0EA1B6"] forState:0];
        self.moreBtn = moreButton;
        UIView *bottomView = [[UIView alloc]initWithFrame:self.familyGroupFrame.bottomViewF];
        bottomView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.contentView addSubview:bottomView];
        self.bottomView = bottomView;
    }
    return self;
}

-(void)settingtData
{
    FamilyGroup *familyGroup = self.familyGroupFrame.familyGroup;
    TopicContentModel *topicModel = [[TopicContentModel alloc]initWithDictionary:familyGroup.topic error:nil];
    //创建头像
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:topicModel.head_url] placeholderImage:[UIImage imageNamed:@"picture1.png"]];
    
    
    //创建昵称
    self.nameLabel.text = topicModel.nick_name;
    //    self.tribeLabel.text = topicModel.tribe_name;
    NSString *text = [NSString stringWithFormat:@"发布于:%@",topicModel.tribe_name];
    //    NSMutableAttributedString *tmpText = [[NSMutableAttributedString alloc] initWithString:text];;
    //    [tmpText addAttribute:NSFontAttributeName
    //                    value:[UIFont systemFontOfSize:30.0f]
    //                    range:NSMakeRange(0, 3)];
    //    [tmpText addAttribute:NSForegroundColorAttributeName
    //                    value:[UIColor redColor]
    //                    range:NSMakeRange(0, 3)];
    self.tribeLabel.text = text;
    
    //创建正文
    self.shuoshuotextLabel.text = topicModel.content_desc;
    
    NSDictionary *tmpDic = [topicModel.mediaList objectAtIndex:0];
    [self.bigImg sd_setImageWithURL:[NSURL URLWithString:[tmpDic objectForKey:@"media_url"]] placeholderImage:[UIImage imageNamed:@""]];
    self.bigImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
    [self.bigImg addGestureRecognizer:tap];
    self.bigImg.tag = imageTag;
    //创建配图
    for (int i = 0; i < [topicModel.mediaList count]; i++) {
        UIImageView *pictureView = [[UIImageView alloc]init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
        [pictureView addGestureRecognizer:tap];
        pictureView.tag = imageTag + i;
        //NSLog(@"%ld",(long)pictureView.tag);
        pictureView.userInteractionEnabled = YES;
        [self.contentView addSubview:pictureView];
        [self.picturesView addObject:pictureView];
        NSString *imgName = [[topicModel.mediaList objectAtIndex:i]objectForKey:@"media_url"];
        [((UIImageView *)[self.picturesView objectAtIndex:i])sd_setImageWithURL:[NSURL URLWithString:imgName] placeholderImage:[UIImage imageNamed:@""]];
    }
    
    //时间戳
    self.timeLabel.text = topicModel.publish_tm;
    //创建分享
    
    //创建评论
    for (int i = 0; i < [familyGroup.contentCmtList count]; i++) {
        UILabel *replyLabel = [[UILabel alloc]init];
        replyLabel.font = replyFont;
        replyLabel.numberOfLines = 0;
        
        NSString *userName = [[familyGroup.contentCmtList objectAtIndex:i]objectForKey:@"user_nike_name"];
        
        NSString *commentString = [[familyGroup.contentCmtList objectAtIndex:i]objectForKey:@"comment"];
        NSString *searchText = [NSString stringWithFormat:@"%@:%@",userName,commentString];
        //把评论的姓名变色
        NSRange range = NSMakeRange(0,userName.length);
        if (range.length != NSNotFound) {
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:searchText];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:104/255.0 green:109/255.0 blue:248/255.0 alpha:1.0] range:NSMakeRange(0, range.length + 1)];
            replyLabel.attributedText = str;
        }
        else
        {
            NSString *userName = [[familyGroup.contentCmtList objectAtIndex:i]objectForKey:@"user_nike_name"];
            NSString *commentString = [[familyGroup.contentCmtList objectAtIndex:i]objectForKey:@"comment"];
            NSString *searchText = [NSString stringWithFormat:@"%@%@",userName,commentString];
            replyLabel.text = searchText;
        }
        [self.contentView addSubview:replyLabel];
        [self.replysView addObject:replyLabel];
    }
    
    NSString *str = [NSString stringWithFormat:@"查看全部%ld条评论",familyGroup.contentCmtList.count];
    [self.moreBtn setTitle:str forState:UIControlStateNormal];
    [self.moreBtn setFont:[UIFont systemFontOfSize:13]];
    [self.moreBtn addTarget:self action:@selector(seeMoreMarks:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)seeMoreMarks:(UIButton *)sender{
    
    [self.delegate myTabVClick:self];
}
-(NSMutableArray *)picturesView
{
    if (!_picturesView) {
        _picturesView = [[NSMutableArray alloc]init];
    }
    return _picturesView;
    
}

-(NSMutableArray *)replysView
{
    if (!_replysView) {
        _replysView = [[NSMutableArray alloc]init];
    }
    return _replysView;
}
-(void)settingFrame
{
    self.iconView.frame = self.familyGroupFrame.iconF;
    self.iconView.layer.cornerRadius = self.iconView.frame.size.width/2;
    self.iconView.layer.masksToBounds = YES;
    self.nameLabel.frame = self.familyGroupFrame.nameF;
    
    self.tribeLabel.frame = self.familyGroupFrame.tribeNameF;
    self.guanzhuBtn.frame = self.familyGroupFrame.guanzhuBtnF;
    self.shuoshuotextLabel.frame = self.familyGroupFrame.shuoshuotextF;
    self.bigImg.frame = self.familyGroupFrame.bigPictureF;
    for (int i = 0; i < [self.familyGroupFrame.picturesF count]; i++) {
        ((UIImageView *)[self.picturesView objectAtIndex:i]).frame = [((NSValue *)[self.familyGroupFrame.picturesF objectAtIndex:i]) CGRectValue];
    }
    for (int i = 0; i < [self.familyGroupFrame.replysF count]; i++) {
        ((UILabel *)[self.replysView objectAtIndex:i]).frame = [(NSValue *)[self.familyGroupFrame.replysF objectAtIndex:i] CGRectValue];
    }
    self.timeLabel.frame = self.familyGroupFrame.timeF;
    self.replyButton.frame = self.familyGroupFrame.replyF;
    self.shareButton.frame = self.familyGroupFrame.shareF;
    self.dianzanBtn.frame = self.familyGroupFrame.dianzanF;
    self.replyBackgroundView.frame = self.familyGroupFrame.replyBackgroundF;
    self.moreBtn.frame = self.familyGroupFrame.moreReplysF;
    self.bottomView.frame = self.familyGroupFrame.bottomViewF;
}

-(void)setFamilyGroupFrame:(FamilyGroupFrame *)familyGroupFrame
{
    _familyGroupFrame = familyGroupFrame;
    [self removeOldPicturesAndReplys];
    [self settingtData];
    [self settingFrame];
}

//防止cell重叠
-(void)removeOldPicturesAndReplys
{
    for(int i = 0;i < [self.picturesView count];i++)
    {
        UIImageView *pictureView = [self.picturesView objectAtIndex:i];
        if (pictureView.superview) {
            [pictureView removeFromSuperview];
        }
    }
    for (int i = 0; i < [self.replysView count]; i++) {
        UILabel *replyView = [self.replysView objectAtIndex:i];
        if (replyView.superview) {
            [replyView removeFromSuperview];
        }
    }
    [self.picturesView removeAllObjects];
    [self.replysView removeAllObjects];
}

-(void)tapImageView:(UITapGestureRecognizer *)tap
{
    NSArray *array = [self.familyGroupFrame.familyGroup.topic objectForKey:@"mediaList"];
    NSMutableArray *muArr = [NSMutableArray new];
    for (NSDictionary *tmpDic in array) {
        if ([tmpDic.allKeys containsObject:@"media_url"]) {
            NSString *imgStr =  [tmpDic objectForKey:@"media_url"];
            [muArr addObject:imgStr];
        }
    }
    self.imageBlock(muArr,tap.view.tag);
}


@end
